package com.ms.common.utils;

import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.UUID;

public class RandomUtil {

    public static final String allChar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    public static final String letterChar = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    public static final String numberChar = "0123456789";

    public static final String[] enLetterE = new String[]{
            "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
            "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"
    };

    public RandomUtil() {
    }

    public static String generateString(int length) {
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        int len = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".length();

        for (int i = 0; i < length; ++i) {
            sb.append("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
                    .charAt(random.nextInt(len)));
        }

        return sb.toString();
    }

    public static String generateString(int length, String numberChar) {
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        int len = numberChar.length();

        for (int i = 0; i < length; ++i) {
            sb.append(numberChar.charAt(random.nextInt(len)));
        }

        return sb.toString();
    }

    public static String generateMixString(int length) {
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < length; ++i) {
            sb.append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".charAt(random.nextInt("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".length())));
        }
        return sb.toString();
    }

    public static String generateLowerString(int length) {
        return generateMixString(length).toLowerCase();
    }

    public static String generateUpperString(int length) {
        return generateMixString(length).toUpperCase();
    }

    public static String generateZeroString(int length) {
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < length; ++i) {
            sb.append('0');
        }

        return sb.toString();
    }

    public static String toFixedLengthString(int num, int fixedLength) {
        StringBuilder sb = new StringBuilder();
        String strNum = String.valueOf(num);
        if (fixedLength - strNum.length() >= 0) {
            sb.append(generateZeroString(fixedLength - strNum.length()));
            sb.append(strNum);
            return sb.toString();
        } else {
            throw new RuntimeException("将数字" + num + "转化为长度为" + fixedLength + "的字符串发生异常!");
        }
    }

    private static String toFixedLengthStringByUUID(int length) {
        UUID uuid = UUID.randomUUID();
        return uuid.toString().replace("-", "").substring(0, length);
    }

    public static int getRandomNumber32() {
        Random random = new Random();
        return random.nextInt(Integer.MAX_VALUE);
    }

    /**
     * 生成短信验证码 6位
     *
     * @return 短信验证码
     */
    public static String createVerifyCode() {
        Random random = new Random();
        int intCode = random.nextInt(999999);
        return String.format("%06d", intCode);
    }

    /**
     * 生成大写英文字母随机编码
     *
     * @param number 编码长度
     * @return 生成大写英文字母随机编码
     */
    public static String createEnCode(Integer number) {
        List list = Arrays.asList(enLetterE);
        Random r = new Random();
        StringBuilder numEn = new StringBuilder();
        for (int i = 0; i < number; i++) {
            int ran1 = r.nextInt(list.size());
            numEn.append(list.get(ran1));
        }
        return numEn.toString();
    }

}
